package com.geeke.wf.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.geeke.wf.service.WfUtils;

@RestController
@RequestMapping(value = "/wf/process-definition")
public class ProcessDefinitionController {
    
    @Autowired
	private WfUtils<?> wfUtils;

    @GetMapping("/{id}/startForm")
    public ResponseEntity<JSONObject> startForm(@PathVariable("id") String id) {
    	String formKey = wfUtils.getStartForm(id);
        JSONObject json = new JSONObject();
        json.put("key", formKey);
        json.put("contextPath", null);

    	return ResponseEntity.ok(json);
    }


    @GetMapping("/key/{key}/tenant-id/{tenantId}/startForm")
    public ResponseEntity<JSONObject> startFormByKey(@PathVariable("key") String key, @PathVariable("tenantId") String tenantId) {
    	String processId = wfUtils.getProcessIdByKey(key, tenantId);
        if(processId == null) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("type", "RestException");
            jsonObject.put("message", "No matching process definition with key: " + key + " and tenant-id: " + tenantId);
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(jsonObject);
        }
    	return startForm(processId);
    }

    @GetMapping("/{id}/xml")
    public ResponseEntity<JSONObject> getProcessBpmn(@PathVariable("id") String id) {
    	String bpmn20Xml = wfUtils.getProcessDefinitionBpmn20Xml(id);
        JSONObject json = new JSONObject();
        json.put("id", id);
        json.put("bpmn20Xml", bpmn20Xml);

    	return ResponseEntity.ok(json);
    }

    @GetMapping("/key/{key}/tenant-id/{tenantId}/xml")
    public ResponseEntity<JSONObject> getProcessBpmnByKey(@PathVariable("key") String key, @PathVariable("tenantId") String tenantId) {
    	String processId = wfUtils.getProcessIdByKey(key, tenantId);
        if(processId == null) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("type", "RestException");
            jsonObject.put("message", "No matching process definition with key: " + key + " and tenant-id: " + tenantId);
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(jsonObject);
        }
    	return getProcessBpmn(processId);
    }
    
    @GetMapping("/{id}/diagram")
    public void getProcessDefinitionDiagram(@PathVariable("id") String id, HttpServletResponse response, HttpServletRequest request) throws IOException {
        // 浏览器缓存变量有值，直接放回304
        String modifiedSice = request.getHeader("if-modified-since");
        if(modifiedSice != null) {
            response.setStatus(HttpStatus.NOT_MODIFIED.value());
            return;
        }

        Map<String, Object> map = wfUtils.getProcessDefinitionDiagram(id);
        response.setHeader("Content-Type", (String)map.get("type"));
        response.setHeader("Content-Disposition", "attachment; filename=" + (String)map.get("fileName"));
        response.addHeader("Last-Modified", id);    // 设置浏览器缓存更新变量

        OutputStream os = response.getOutputStream();
        os.write((byte[])map.get("content"));
        os.flush();
        os.close();
    }

}
